<?php

namespace App\Manage;

use App\Service\Base\Manage;
use App\Models\SiteCate;
use App\Service\Tree;

class SiteCateManage extends Manage
{
    protected $channel;

    // 栏目管理
    public function initialize()
    {
        parent::initialize();

        $this->model = 'SiteCate';
    }

    // 父级
    // 添加，修改，删除，列表，停用 操作

    // 返回列表数据
    public function onLists()
    {
        $lists = $this->getLists();

        return $lists;
    }

    // 获取列表
    protected function getLists()
    {
        $where = $this->listWhere();
        $model = new SiteCate();

        // 查询（列表展示）
        /*$page = $this->page;
        $rows = $this->rows;
        if($where){
            $result = $model->listPage($where, $page, $rows, "id DESC");
        } else{
            $result = $model->isCache(true)->listPage($where, $page, $rows, "id DESC");
        }*/

        // 树状制作
        $lists = $model->isCache(true)->getAll($where, 'rank ASC');
        if(!empty($lists)){
            $tree  = new Tree();
            $lists = $tree->toTree($lists); // 树状
        }
        //$total  = count($lists);
        //$result = ['total' => $total, 'page' => 1, 'rows' => $this->rows, 'list' => $lists,];

        return $lists;
    }

    // 排序
    public function onRank()
    {
        $opt    = $this->option();
        $_value = $opt->getData('value', null, 'intval');
        $_id    = $opt->getData('id', 0, 'intval');
        if(is_null($_value)){
            return $this->_returnFail('状态必须填写！！');
        }
        if(empty($_id)){
            return $this->_returnFail('请指定操作记录！！');
        }

        $model  = new SiteCate();
        $result = $model->oneEdit(['id' => $_id], ['rank' => $_value]);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }


    // 删除（软删除）
    public function onDelete()
    {
        $opt = $this->option();
        $id  = $opt->getData("id", 0, 'intval');
        if(empty($id)){
            return $this->_returnFail('请指定删除记录！');
        }

        // 删除的时候：如果有子级不能删除
        if(!empty($data['pid'])){
            $cateDb = new SiteCate();
            $ids    = $cateDb->getChildIds($id);
            if($ids){
                return $this->_returnFail('该栏目有子级不能被删除！');
            }
        }

        $where = ['id' => $id];
        $model = $this->checkModel($this->model);
        if($model === false){
            return false;
        }

        // 执行
        $result = $model->isCache(true)->oneDelete($where);
        //$result = $model->isCache(true)->oneEdit($where, ['status' => -1]);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 列表查询
    protected function listWhere()
    {
        $opt     = $this->option();
        $where   = [['status', '>=', 0]];
        $keyword = $opt->getData('keyword', null, 'trim');
        if($keyword){
            $where[] = $this->orWhere('title|keyword|description', 'like', $keyword);
        }

        return $where;
    }

    // 添加
    protected function _before_add()
    {
        $opt  = $this->option();
        $data = [
            //'id'     => $opt->getData("id", 0, 'intval'),
            'pid'        => $opt->getData("pid", 0, 'intval'),
            'rank'       => $opt->getData("rank", 0, 'intval'),
            'en_title'   => $opt->getData("en_title", '', 'trim'),
            'cn_title'   => $opt->getData("cn_title", '', 'trim'),
            'tc_title'   => $opt->getData("tc_title", '', 'trim'),
            'en_keyword' => $opt->getData("en_keyword", null, 'trim'),
            'cn_keyword' => $opt->getData("cn_keyword", null, 'trim'),
            'tc_keyword' => $opt->getData("tc_keyword", null, 'trim'),
            'status'     => $opt->getData("status", 1, 'intval'),
            //'ctime'  => $opt->getData("ctime", 0, 'intval'),
            //'mtime'  => $opt->getData("mtime", 0, 'intval'),
        ];

        // 判断
        if(empty($data['en_title']) && empty($data['cn_title']) && empty($data['tc_title'])){
            return $this->_returnFail('请填写分类名称！');
        }

        return $data;
    }

    // 修改
    protected function _before_edit()
    {
        $opt  = $this->option();
        $id   = $opt->getData("id", 0, 'intval');
        $data = [
            //'id'     => $opt->getData("id", 0, 'intval'),
            'pid'        => $opt->getData("pid", 0, 'intval'),
            'rank'       => $opt->getData("rank", 0, 'intval'),
            'en_title'   => $opt->getData("en_title", '', 'trim'),
            'cn_title'   => $opt->getData("cn_title", '', 'trim'),
            'tc_title'   => $opt->getData("tc_title", '', 'trim'),
            'en_keyword' => $opt->getData("en_keyword", null, 'trim'),
            'cn_keyword' => $opt->getData("cn_keyword", null, 'trim'),
            'tc_keyword' => $opt->getData("tc_keyword", null, 'trim'),
            'status'     => $opt->getData("status", 1, 'intval'),
            //'ctime'  => $opt->getData("ctime", 0, 'intval'),
            //'mtime'  => $opt->getData("mtime", 0, 'intval'),
        ];

        // 判断
        if(empty($id)){
            return $this->_returnFail('请指定要修改的记录！');
        }
        if(empty($data['en_title']) && empty($data['cn_title']) && empty($data['tc_title'])){
            return $this->_returnFail('请填写分类名称！');
        }


        // 修改的时候：如果有父级 则父级不能是当前id 也不能是当前id的子级
        if(!empty($data['pid'])){
            $pid = $data['pid'];
            if($id == $pid){
                return $this->_returnFail('修改父级不能是自己！');
            }

            // 获取当前的子级
            $cateDb = new SiteCate();
            $ids    = $cateDb->getChildIds($id);
            if($ids && in_array($pid, $ids)){
                return $this->_returnFail('修改父级不能是自己的子级！');
            }
        }

        return $data;
    }

}


